package com.gome.han.bigdata.spark.streaming.qucikstart

import org.apache.spark.SparkConf
import org.apache.spark.streaming.dstream.{DStream, ReceiverInputDStream}
import org.apache.spark.streaming.{Seconds, StreamingContext}

/**
 * @author Hanpeng
 * @date 2021/1/12 13:48
 * @description:
 */
object QuickStart {
  def main(args: Array[String]): Unit = {
    val conf = new SparkConf().setMaster("local[2]").setAppName("NetworkWordCount");
    val ssc = new StreamingContext(conf, Seconds(1));
    val lines: ReceiverInputDStream[String] = ssc.socketTextStream("localhost", 9999);
    val words: DStream[String] = lines.flatMap(_.split(" "));
    val pairs = words.map(word => (word, 1));
    val wordCounts = pairs.reduceByKey(_ + _);
    wordCounts.print();
    ssc.start()             // Start the computation
    ssc.awaitTermination()  // Wait for the computation to terminate;
  }
}
